<div class="">
    <ol class="breadcrumb">
        <li><i class="fa fa-anchor"></i>
            &nbsp;厂商管理
        </li>
        <li>类型设置</li>
        <li style="float: right"><a href="javascript:history.go(-1);" style="color: #ffffff">返回</a></li>
    </ol>
    <div class="col-xs-12 info-center">
        <div class="bs-callout bs-callout-warning">
            <h4>
                系统使用单位:
                <strong>{:C('COLLEGE_NAME')}</strong>
            </h4>
            <span style="float: right"><i class="fa fa-asterisk required-fa"></i>&nbsp;<strong class="alert-warning">以下分类中,署名【<strong class="alert-danger">未知</strong>】分类为系统默认添加,请勿删除！</strong></span>
            <div class="col-xs-4 search-col">
                <form id="add_category" action="__URL__/addCategory" method="post">
                    <div class="input-group">
                        <input type="hidden" name="id" value="{$_GET['id']}"/>
                        <input type="text" class="form-control" name="category" id="category">
                    <span class="input-group-btn">
                    <button class="btn btn-default" type="submit"><i class="fa fa-plus"></i>&nbsp;新增一级分类</button>
                    </span>
                    </div>
                </form>
            </div>
            <div class="clearfix">
            </div>
        </div>
    </div>
    <div class="col-xs-12 info-center">
        <div class="panel panel-mcit">
            <div class="panel-heading">
                <i class="fa fa-list"></i>
                &nbsp;&nbsp;分类列表
            </div>
            <table class="table table-bordered ">
                <tbody id="DataTableContent">
                <tr id="school_list">
                    <th width="30%">
                        名称
                    </th>
                    <th width="15%">
                        操作
                    </th>
                </tr>
                <volist name="device" id="data">
                    <empty name="data[area]">
                        <td colspan="99" style="text-align:center" class="danger">
                            <strong><i class="fa fa-meh-o"></i>
                                &nbsp;&nbsp;请添加一级分类
                            </strong>
                        </td>
                        <else/>
                        <volist name="data[area]" id="data">
                            <td>
                                <p class="school_text">
                                    {$data.name}
                                </p>
                                <div class="col-xs-6" style="display:none">
                                    <div class="input-group">
                                        <input type="text" class="form-control input-sm school_input" value="{$data.name}" name="category">
                            <span class="input-group-btn">
                            <a class="btn btn-default btn-sm goto_edit" data-old="{$data.name}" data-id="{$data.id}"><i class="fa fa-refresh"></i>&nbsp;保存</a>
                            </span>
                                    </div>
                                </div>
                            <td>
                                <a class="btn btn-sm btn-warning children"><i class="fa fa-plus-square-o"></i>&nbsp;下级分类设置</a>
                                <a class="btn btn-sm btn-warning editCategory"><i class="fa fa-edit"></i>&nbsp;修改分类名</a>
                                <a class="btn btn-sm btn-danger delCategory" data-id="{$data.id}" data-old="{$data.pid}"><i class="fa fa-trash-o"></i>&nbsp;删除</a>
                            </td>
                            </tr>
                            <tr class="mcit_datail" id="mcit_datail{$data.id}" style="display:none">
                                <td colspan="4" class="bs-example-bg-classes">
                                    <div class="col-xs-6">
                                        <div class="input-group">
                                            <input type="text" class="form-control category2_input" name="category2">
                                        <span class="input-group-btn">
                                        <a class="btn btn-default add_category2" data-parent="{$data.manufacturer_id}"><i class="fa fa-plus"></i>&nbsp;新增该分类下二级分类</a>
                                        </span>
                                            <input type="hidden" name="category" value="{$data.id}">
                                        </div>
                                    </div>
                                    <div class="clearfix">
                                    </div>
                                    <div class="col-xs-12">
                                        <table class="table table-bordered " style="background-color: #ddd;border: 1px solid #fff;">
                                            <tbody id="DataTableContent1">
                                            <tr class="c2_list">
                                                <th width="50%">
                                                    二级分类名称
                                                </th>
                                                <th width="15%">
                                                    二级分类操作
                                                </th>
                                            </tr>
                                            <volist name="data['area']" id="vo">
                                                <tr class="tr-school">
                                                    <td>
                                                        <p class="school_text">
                                                            {$vo.name}
                                                        </p>
                                                        <div class="col-xs-6" style="display:none">
                                                            <div class="input-group">
                                                                <input type="text" class="form-control input-sm school_input" value="{$vo.name}" name="category">
                                                            <span class="input-group-btn">
                                                            <a class="btn btn-default btn-sm goto_edit" data-old="{$vo.name}" data-id="{$vo.id}"><i class="fa fa-refresh"></i>&nbsp;保存</a>
                                                            </span>
                                                            </div>
                                                        </div>
                                                    <td>
                                                        <a class="btn btn-sm btn-warning editCategory"><i class="fa fa-edit"></i>&nbsp;修改分类名</a>
                                                        <a class="btn btn-sm btn-danger delCategory" data-id="{$vo.id}"><i class="fa fa-trash-o"></i>&nbsp;删除</a>
                                                    </td>
                                                </tr>
                                            </volist>
                                            </tbody>
                                        </table>
                                    </div>
                                </td>
                            </tr>
                        </volist>
                    </empty>
                </volist>
                </tbody>
            </table>
        </div>
    </div>
</div>
<import file="js.sortable" />
<script type="text/javascript">
    var addMajorObj;
    var removeMajorObj;
    var editObj;
    var removeSchoolObj;
    $(document).ready(function () {
        //新增一级类型
        $("#add_category").ajaxForm({
            dataType:'json',
            clearForm:true,
            beforeSubmit:function(){
                if($("#category").val()==''){
                    alertify.error("请填写要添加的分类名称");
                    return false;
                }
                return true
            },
            success:function(data){
                if(data.state==100){
                    alertify.success(data.msg);
                    location.href = data.url;
                }else{
                    alertify.error(data.msg);
                }
            }
        });
        //编辑
        $("#DataTableContent").on("click",'.editCategory',function(){
            var obj  = $(this).parent().prev();
            obj.find('.school_text').hide();
            obj.find('.col-xs-6').show();
        });
        //修改类型
        $("#DataTableContent").on("click",'.goto_edit',function(){
            editObj = $(this).parents('.col-xs-6');
            var category = $(this).parent().prev().val();
            var id       = $(this).attr('data-id');
            var old      = $(this).attr('data-old');
            //alertify.success(category);
            //alertify.success(id);
            //alertify.success(old);
            if(old == category){
                alertify.success("保存成功");
                editObj.hide();
                editObj.prev().show();
            }else{
                $.ajax({
                    type: "POST",
                    url:  '{:U("Fac/editCategory")}',
                    data: {id:id,category:category},
                    dataType: "json",
                    success: function(data){
                        if(data.state==100){
                            alertify.success(data.msg);
                            editObj.hide();
                            editObj.find('.goto_edit').attr('data-old',data.category);
                            editObj.prev().html(data.category);
                            editObj.prev().show();
                        }else{
                            alertify.error(data.msg);
                        }
                    }
                });
            }
        });
        //下跳
        $("#DataTableContent").on("click",'.children',function(){
            $(this).parents('tr').next().fadeToggle();
        });
        //新增二级类型
        $("#DataTableContent").on("click",'.add_category2',function(){
            var category2  = $(this).parent().prev().val();
            $(this).parent().prev().val('');
            var category = $(this).parent().next().val();
            var parent_id = $(this).attr("data-parent");
            addMajorObj = $(this).parents('.col-xs-6').next().next().find('.c2_list');
            //alertify.success(category);
            //alertify.success(category2);
            //alertify.success(parent_id);
            if( category2=='' ){
                alertify.error("请填写要添加的分类名");
            }else{
                $.ajax({
                    type: "POST",
                    url:  '{:U("Fac/addCategory2")}',
                    data: {category:category,category2:category2,parent_id:parent_id},
                    dataType: "json",
                    success: function(data){
                        if(data.state==100){
                            alertify.success(data.msg);
                            // var addStr = '<div class="col-xs-6"><p class="bg-info"><strong>'+data.name+'</strong><a class="btn btn-link btn-xs pull-right del_major" data-id="'+data.id+'"><i class="fa fa-trash-o"></i>&nbsp;删除</a></p></div>';
                            var addStr='<tr class="tr-school">' +
                                    '<td><p class="school_text">'+data.name+'</p>' +
                                    '<div class="col-xs-6" style="display:none">' +
                                    '<div class="input-group">' +
                                    '<input type="text" class="form-control input-sm school_input" value="'+data.name+'" name="category">' +
                                    ' <span class="input-group-btn">' +
                                    '<a class="btn btn-default btn-sm goto_edit" data-old="'+data.name+'" data-id="'+data.id+'">' +
                                    '<i class="fa fa-refresh"></i>&nbsp;保存</a>' +
                                    '</span></div></div></td>' +
                                    '<td><a class="btn btn-sm btn-warning editCategory"><i class="fa fa-edit"></i>&nbsp;修改分类名</a>&nbsp;' +
                                    '<a class="btn btn-sm btn-danger delCategory" data-id="'+data.id+'"><i class="fa fa-trash-o"></i>&nbsp;删除</a></td></tr>';
                            addMajorObj.after(addStr);
                        }else{
                            alertify.error(data.msg);
                        }
                    }
                });
            }
        });
        //删除类型
        $("#DataTableContent").on("click",'.delCategory',function(){
            var objThis  = $(this);
            var id   = $(this).attr('data-id');
            var old  = $(this).attr('data-old');
            //alertify.success(id);
            //alertify.success(old);
            alertify.confirm("确定要删除此分类？", function (e) {
                if (e) {
                    $.ajax({
                        type: "POST",
                        url:  '{:U("Fac/delCategory")}',
                        data: {id:id,pid:old},
                        dataType: "json",
                        success: function(data){
                            if(data.state==100){
                                alertify.success(data.msg);
                            }else{
                                alertify.error(data.msg);
                            }
                            location.reload();
                        }
                    });
                }else {
                    alertify.error("取消操作！");
                }
            }, "mcit_default");
        });
    });
</script>